/*
 * CatalogoListProductos.java
 *
 * Created on 23 de enero de 2007, 01:12 PM
 */

package inc.jmc.cyberboard.ztrashcan;

import inc.jmc.cyberboard.common.NumFormat;
import inc.jmc.cyberboard.dao.Dao;
import inc.jmc.cyberboard.dao.ProductosDao;
import inc.jmc.cyberboard.entitybeans.Productos;
import java.text.NumberFormat;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

/**
 *
 * @author  Juan Miguel Olguin Salguero
 */
public class CatalogoListProductos extends javax.swing.JPanel {
    
    
    DefaultListModel dlm=new DefaultListModel();
    
    /** Creates new form CatalogoListProductos */
    public CatalogoListProductos() {
        initComponents();
        
        try{
            loadProductsBD();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            dlm=new DefaultListModel();
            this.updateTableUI(dlm);
        
        }
    }
    
    
    public void updateTableUI(DefaultListModel dlm )
    {
        jScrollPane1.setViewportView(productosList);
        if(dlm.size()>0)
            this.productosList.getSelectionModel().setSelectionInterval(0,0);
        
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        productosList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        productosList.setBackground(new java.awt.Color(238, 253, 255));
        productosList.setFont(new java.awt.Font("Courier New", 1, 14));
        productosList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(productosList);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents
    
    
    public String blankString(String cadena,int length)
    {
    	StringBuffer ret=new StringBuffer(cadena);
    	if(ret.length()<length)
    	{
    		for(int i=ret.length();i<length;++i)
    		{
    			ret.append(" ");
    		}
    	}
    	return ret.substring(0,length);
    }
    public String blankNumber(float number1,int length)
    {
        StringBuffer ret=new StringBuffer();
        String number=NumFormat.money(number1);
    	if(number.length()<length)
    	{
    		for(int i=0;i<(length-number.length());++i)
    		{
    			ret.append(" ");
    		}
                ret.append(number);
    	}
    	return ret.substring(0,length);
    }
    public void loadProductsBD()throws Exception
    {
                ProductosDao recsDao=new ProductosDao();
                java.sql.Connection conn=Dao.getConnection();
                java.util.ArrayList arry=recsDao.getAllProductos(conn);
                conn.close();
                for(int i=0;i<arry.size();++i)
                {
                    Productos record=(Productos)arry.get(i);
                    StringBuffer addstr=new StringBuffer();
                    addstr.append(blankString(record.getNombreCorto(),30));
                    addstr.append(blankNumber(record.getPrecio().floatValue(),10));
                    //addstr.append(blankString(""+record.getDescripcion(),30));
                    dlm.addElement(addstr);
                }
                this.updateTableUI(dlm);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList productosList;
    // End of variables declaration//GEN-END:variables

    public JList getProductosList() {
        return productosList;
    }

    public void setProductosList(JList productosList) {
        this.productosList = productosList;
    }


}
